//
//  HWTakeALookPageFooter.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/3.
//

import UIKit

class HWTakeALookPageFooter: UICollectionReusableView {
    
    var lineView:UIView!
    
    var lookImageView:UIImageView!
    var commentImageView:UIImageView!
    var zanImageView:UIImageView!
    
    var lookLabel:UILabel!
    var commentLabel:UILabel!
    var zanLabel:UILabel!
    
    var lookButton:UIButton!
    var commentButton:UIButton!
    var zanButton:UIButton!
    
    var comLabel:UILabel!
    var commentNumLabel:UILabel!
    var textTagCollectionView : TTGTextTagCollectionView!
    
    var zanButtonClick:((_:String)->())?
    var jumpToDetail:((_:HWGameCircleModel)->())?
    var model:HWGameCircleModel?{
        didSet{
            lookLabel.text = model?.look
            commentLabel.text = model?.pinglun
            zanLabel.text = model?.zan
            if model?.is_zan == true {
                zanImageView.image = UIImage(named: "zanSelect")
            }else{
                zanImageView.image = UIImage(named: "zanNormal")
            }
            if model?.huati.count ?? 0 > 0  {
                textTagCollectionView.snp.updateConstraints { make in
                    make.height.equalTo(26)
                }
                textTagCollectionView .removeAllTags()
                for txt in model!.huati {
                    let content = TTGTextTagStringContent.init(text: txt)
                    content.textColor = UIColor.colorWithHexString(hexString: "#333300")
                    content.textFont = UIFont.systemFont(ofSize: 12)
                    
                    let selectContent = TTGTextTagStringContent.init(text: txt)
                    selectContent.textColor = UIColor.colorWithHexString(hexString: "#333300")
                    selectContent.textFont = UIFont.systemFont(ofSize: 12)
                    
                    let normalStyle = TTGTextTagStyle.init()
                    normalStyle.backgroundColor = UIColor.colorWithHexString(hexString: "#F5F5F5")
                    normalStyle.cornerRadius = 4
                    normalStyle.shadowColor = UIColor.white
                    normalStyle.extraSpace = CGSize.init(width: 18, height: 7)
                    
                    let selectedStyle = TTGTextTagStyle.init()
                    selectedStyle.backgroundColor = UIColor.colorWithHexString(hexString: "#F5F5F5")
                    selectedStyle.shadowColor = UIColor.white
                    selectedStyle.cornerRadius = 4
                    selectedStyle.extraSpace = CGSize.init(width: 18, height: 7)
                    
                    let tag = TTGTextTag.init()
                    tag.content = content
                    tag.selectedContent = selectContent
                    tag.style = normalStyle
                    tag.selectedStyle = selectedStyle
                    //                tag.selected = mod.isSelect
                    
                    textTagCollectionView.addTag(tag)
                }
            }else{
                textTagCollectionView.snp.updateConstraints { make in
                    make.height.equalTo(0)
                }
            }
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWTakeALookPageFooter {
    private func initUI() -> Void {
        backgroundColor = .white
        
        textTagCollectionView = TTGTextTagCollectionView.init(frame: .zero)
        textTagCollectionView.delegate = self
        textTagCollectionView.backgroundColor = .white
        textTagCollectionView.alignment = TTGTagCollectionAlignment.left;
        textTagCollectionView.numberOfLines = 1
        textTagCollectionView.manualCalculateHeight = true;
        textTagCollectionView.scrollDirection = .horizontal
        textTagCollectionView.showsHorizontalScrollIndicator = false
        textTagCollectionView.preferredMaxLayoutWidth = screenWidth-30;
        
        addSubview(textTagCollectionView)
        
        
        lineView = UIView()
        lineView.backgroundColor = UIColor.colorWithHexString(hexString: "#E6E7E8")
        addSubview(lineView)
        
        lookImageView = UIImageView()
        lookImageView.image = UIImage(named: "lookImage")
        addSubview(lookImageView)
        
        commentImageView = UIImageView()
        commentImageView.image = UIImage(named: "commentImage")
        addSubview(commentImageView)
        
        zanImageView = UIImageView()
        zanImageView.image = UIImage(named: "zanNormal")
        addSubview(zanImageView)
        
        lookLabel = HWLabel.createLabel(title: "-", color: "#666666", font: 12)
        addSubview(lookLabel)
        
        commentLabel = HWLabel.createLabel(title: "-", color: "#666666", font: 12)
        addSubview(commentLabel)
        
        zanLabel = HWLabel.createLabel(title: "-", color: "#666666", font: 12)
        addSubview(zanLabel)
        
        lookButton = .init(type: UIButton.ButtonType.custom)
        addSubview(lookButton)
        
        commentButton  = .init(type: UIButton.ButtonType.custom)
        commentButton.addTarget(self, action: #selector(commentButtonClick(_:)), for: UIControl.Event.touchUpInside)
        addSubview(commentButton)
        
        zanButton = .init(type: UIButton.ButtonType.custom)
        zanButton.addTarget(self, action: #selector(zanButtonClick(_:)), for: UIControl.Event.touchUpInside)
        addSubview(zanButton)
        
        let tapBg = UITapGestureRecognizer.init(target: self, action: #selector(tapBg))
        addGestureRecognizer(tapBg)
        
        comLabel = HWLabel.createBoldLabel(title: "评价", color: "#333333", font: 16)
        addSubview(comLabel)
        
        commentNumLabel = HWLabel.createLabel(title: "", color: "#999999", font: 12)
        addSubview(commentNumLabel)
        comLabel.isHidden = true
        commentNumLabel.isHidden = true
    }
    
    private func initFrame() -> Void {
        textTagCollectionView.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(26)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(textTagCollectionView.snp.bottom).offset(10)
            make.height.equalTo(1)
        }
        
        lookImageView.snp.makeConstraints { make in
            make.left.equalTo(30)
            make.top.equalTo(lineView.snp.bottom).offset(10)
        }
        
        lookLabel.snp.makeConstraints { make in
            make.centerY.equalTo(lookImageView)
            make.left.equalTo(lookImageView.snp.right).offset(5)
        }
        
        commentImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(lookImageView)
        }
        
        commentLabel.snp.makeConstraints { make in
            make.centerY.equalTo(commentImageView)
            make.left.equalTo(commentImageView.snp.right).offset(5)
        }
        
        zanLabel.snp.makeConstraints { make in
            make.right.equalTo(-30)
            make.centerY.equalTo(zanImageView)
            
        }
        
        zanImageView.snp.makeConstraints { make in
            make.top.equalTo(lookImageView)
            make.right.equalTo(zanLabel.snp.left).offset(-5)
        }
        
        
        lookButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(lookImageView)
            make.right.equalTo(lookLabel)
        }
        
        commentButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(commentImageView)
            make.right.equalTo(commentLabel)
        }
        
        zanButton.snp.makeConstraints { make in
            make.left.top.bottom.equalTo(zanImageView)
            make.right.equalTo(zanLabel)
        }
        
        comLabel.snp.makeConstraints { make in
            make.top.equalTo(lookImageView.snp.bottom).offset(20)
            make.left.equalTo(15)
        }
        
        commentNumLabel.snp.makeConstraints { make in
            make.bottom.equalTo(comLabel)
            make.left.equalTo(comLabel.snp.right).offset(5)
        }
        
        lookImageView.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
        commentImageView.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
        zanImageView.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
        
        
    }
}

extension HWTakeALookPageFooter:TTGTextTagCollectionViewDelegate{
    func textTagCollectionView(_ textTagCollectionView: TTGTextTagCollectionView!, didTap tag: TTGTextTag!, at index: UInt) {
        //        tagIsSelect = tag.selected
        //        for i in 0..<_model!.datas.count {
        //
        //            textTagCollectionView.updateTag(at: UInt(i), selected: false)
        //            _model?.datas[i].isSelect = false
        //        }
        //        textTagCollectionView.updateTag(at: index, selected: tagIsSelect)
        //        _model?.datas[Int(index)].isSelect = tagIsSelect
        
    }
}

extension HWTakeALookPageFooter {
    @objc func tapBg() -> Void {
        jumpToDetail!(model ?? HWGameCircleModel())
    }
    
    @objc func commentButtonClick(_ button:UIButton) -> Void {
        jumpToDetail!(model ?? HWGameCircleModel())
    }
    
    @objc func zanButtonClick(_ button:UIButton) -> Void {
        zanButtonClick!(model?.id ?? "")
    }
    
    
}
